<template>
  <div class="app-container">
    <div class="search-container">
      <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
        <el-form-item label="库存单位" prop="storageId">
          <drug-storage-info ref="storageIdDeptRef" :clearable="false"
                             :org-id="queryParams.orgId"
                             :value="queryParams.storageId" :value-storage-id="queryParams.storageId"
                             class="import-master" style="width: 185px"
                             @drugStorageChange="storageIdClick"/>
        </el-form-item>
        <el-form-item label="药品名称" prop="drugName">
          <el-input
            v-model="queryParams.drugName"
            clearable
            placeholder="请输入药品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="厂商" prop="firmName">
          <el-input
            v-model="queryParams.firmName"
            clearable
            placeholder="请输入厂商名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="来源" prop="sourceTypeCode">
          <el-select v-model="queryParams.sourceTypeCode" class="width-120" clearable>
            <el-option
              v-for="dict in dictDataMap['drug_inventory_occupy']"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="患者姓名" prop="patientName">
          <el-input
            v-model="queryParams.patientName"
            clearable
            placeholder="请输入患者姓名"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="门诊号" prop="clinicNo">
          <el-input
            v-model="queryParams.clinicNo"
            clearable
            placeholder="请输入门诊号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="住院号" prop="visitNo">
          <el-input
            v-model="queryParams.visitNo"
            clearable
            placeholder="请输入住院号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="处方号" prop="prescNo">
          <el-input
            v-model="queryParams.prescNo"
            clearable
            placeholder="请输入处方号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="医嘱号" prop="orderId">
          <el-input
            v-model="queryParams.orderId"
            clearable
            placeholder="请输入医嘱号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="摆药序号" prop="disNo">
          <el-input
            v-model="queryParams.disNo"
            clearable
            placeholder="请输入摆药序号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          plain
          type="danger"
          @click="handleDelete"
        >
          <template #icon>
            <i-ep-delete/>
          </template>
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="warning"
          @click="handleExport"
        >
          <template #icon>
            <i-ep-download/>
          </template>
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :border="true" :data="drugInventoryOccupyList" :header-cell-style="$headerCellStyle"
              :stripe="true" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column :align="'center'" type="selection" width="55"/>
      <el-table-column :align="'center'" label="来源" prop="sourceTypeName"/>
      <el-table-column :align="'center'" label="库房" prop="storageName"/>
      <el-table-column :align="'center'" label="药品编码" prop="drugCode"/>
      <el-table-column :align="'center'" label="药品名称" prop="drugName" width="260">
        <template #default="scope">
          <span class="ys-css" style="font-weight: bold;">{{ scope.row.drugName }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="药品规格" prop="packageSpec"/>
      <el-table-column :align="'center'" label="占用数量" prop="reductionAmount">
        <template #default="scope">
          <span class="ys-css">{{ scope.row.reductionAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="单位" prop="packageUnitName" width="70"/>
      <el-table-column :align="'center'" :show-overflow-tooltip="true" label="厂商" prop="firmName"/>
      <el-table-column :align="'center'" label="患者姓名" prop="patientName"/>
      <el-table-column :align="'center'" label="门诊号" prop="clinicNo"/>
      <el-table-column :align="'center'" label="住院号" prop="visitNo"/>
      <el-table-column :align="'center'" label="处方号" prop="prescNo"/>
      <el-table-column :align="'center'" label="医嘱号" prop="orderId"/>
      <el-table-column :align="'center'" label="摆药序号" prop="disNo"/>
      <el-table-column :align="'center'" label="市场零售价(元)" prop="retailPrice"/>
      <el-table-column :align="'center'" label="库存分组id" prop="identicalInfoId"/>
      <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作" width="80">
        <template #default="scope">
          <el-tooltip class="item" content="释放占用" effect="dark" placement="top">
            <el-button circle type="danger" @click="handleDelete(scope.row)">
              <template #icon>
                <i-ep-delete/>
              </template>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>
<script lang="ts">
export default {
  name: "DrugInventoryOccupy"
}
</script>

<script lang="ts" setup>
import {
  delDrugInventoryOccupy, selectPageDrugInventoryOccupy
} from "@/api/drug/drugInventoryOccupy";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {useUserStoreHook} from "@/store/modules/user";

const useUserStore = useUserStoreHook();
const {proxy} = getCurrentInstance() as any;

const currentSelectDept = proxy.$currentSelectDept
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;


const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('sys_yes_no', 'drug_inventory_occupy'))
const queryParamsRef = ref()
const storageIdDeptRef = ref()
const drugInventoryOccupyList = ref([]);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  drugInventoryConsumptionId: "",
  patientId: "",
  patientName: "",
  clinicNo: "",
  visitNo: "",
  prescNo: "",
  orderId: "",
  disNo: "",
  storageId: currentSelectDept,
  storageName: "",
  priceId: "",
  stockId: "",
  batchNo: "",
  infoId: "",
  drugCode: "",
  drugName: "",
  packageSpec: "",
  packageUnit: "",
  packageUnitName: "",
  firmId: "",
  firmName: "",
  isType: "",
  applyDate: "",
  sourceTypeCode: "",
  sourceTypeName: "",
  isBatchPresc: "",
  orgId: userInfo.orgId,
  remarks: "",
  updateByName: "",
  identicalInfoId: "",
  createByName: ""
})

/** 查询库存消耗列表 */
function getList() {
  selectPageDrugInventoryOccupy(queryParams.value).then((response: any) => {
    drugInventoryOccupyList.value = response.rows;
    drugInventoryOccupyList.value.forEach((item: any) => {
      item.reductionAmount = item.packSize === '1' ? item.reductionAmount : item.reductionAmount / item.packageNum;
    })
    total.value = response.total;
  });
}

function storageIdClick(obj) {
  queryParams.value.storageId = obj.storageId;
  getList();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  // proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.drugInventoryConsumptionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _drugInventoryConsumptionIds = row.drugInventoryConsumptionId ||
    ids.value;
  ElMessageBox.confirm('是否确认释放占用？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delDrugInventoryOccupy(_drugInventoryConsumptionIds).then(() => {
      getList();
      ElMessage.success("释放占用成功");
    })
  }).catch(() => {
  })
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('drug/drugInventoryOccupy/export', {
    ...queryParams.value
  }, `drugInventoryOccupy_${new Date().getTime()}.xlsx`)
}
onMounted(() => {
  getList();
  if (proxy.$currentSelectDept) {
    nextTick(() => {
      storageIdDeptRef.value.getDrugStorageList()
    })
  }
})
</script>
